package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.PublicBean;

/**
 * Copyright (c)2019
 * author: zdh
 * created on: 2019/1/14 18:12
 * description:
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryHolder> {

    private Context mContext;
    private ArrayList<String> viewData;
    private OnDelListener listener;
    private ArrayList<PublicBean.PublicListBean> publicData;
    private String type;

    public void setOnDelListener(OnDelListener listener) {
        this.listener = listener;
    }


    public interface GhfInterface {
        public void onClick(View view, String line_name, String line_guide, String jaon, int position);
    }

    public void GhfOnClick(GhfInterface ghfInterface) {
        this.ghfInterface = ghfInterface;
    }

    private GhfInterface ghfInterface;

    public HistoryAdapter(Context busSearchHistotyActivity) {
        mContext = busSearchHistotyActivity;

    }

    @Override
    public HistoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.history_view, parent, false);
        return new HistoryHolder(view);
    }

    @Override
    public void onBindViewHolder(HistoryHolder holder, final int position) {
        if (publicData != null) {
            final PublicBean.PublicListBean bean = publicData.get(position);
            holder.historyTv.setText(bean.getLine_name());

            if (bean.getLine_direction() != null) {
                holder.historyDes.setText(bean.getLine_direction());
            } else {
                holder.historyDes.setVisibility(View.GONE);
            }
            if (bean.getType() == 1) {
                holder.line_mark.setVisibility(View.VISIBLE);
            } else {
                holder.line_mark.setVisibility(View.GONE);
            }
            final String json = new Gson().toJson(bean);
            holder.historyLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (ghfInterface != null) {
                        ghfInterface.onClick(v, bean.getLine_name(), bean.getLine_guid(), bean.getLine_direction(), position);
                    }
                }
            });
            holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onDel(position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == publicData ? 0 : publicData.size();
    }

    public void setViewData(ArrayList<String> viewData) {
        this.viewData = viewData;
    }

    public void setPublicData(ArrayList<PublicBean.PublicListBean> publicData, String type) {
        this.type = type;
        this.publicData = publicData;
        notifyDataSetChanged();
    }

    public static class HistoryHolder extends RecyclerView.ViewHolder {

        private final TextView historyTv;
        private final TextView tvDelete;
        private final TextView line_mark;
        private final TextView historyDes;
        private final LinearLayout historyLl;

        public HistoryHolder(View itemView) {
            super(itemView);
            historyTv = (TextView) itemView.findViewById(R.id.history_tv);
            tvDelete = (TextView) itemView.findViewById(R.id.tv_delete);
            historyDes = (TextView) itemView.findViewById(R.id.history_des);
            line_mark = (TextView) itemView.findViewById(R.id.line_mark);
            historyLl = (LinearLayout) itemView.findViewById(R.id.history_rl);
        }
    }



    public interface OnDelListener {
        void onDel(int position);
    }
}
